package IT.DataAccess;

import IT.Entities.Variable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DatVariable implements IDatVariable{
    
    private Variable oVariable;
    private List<Variable> oLista;
    private BDConexion oBase;
    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;    
    
    public List<Variable> Listar() {
        oLista=new ArrayList<Variable>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_VariableListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new Variable( oRst.getInt("cod_var_in"), 
                            oRst.getString("nom_var_vc"), oRst.getString("val_var_vc")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;		 
    }

    public Variable Ind(int nCodigo) {
        oVariable=new Variable();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_VariableInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new Variable( oRst.getInt("cod_var_in"), 
                            oRst.getString("nom_var_vc"), oRst.getString("val_var_vc")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oVariable;
    }

    public void Insertar(Variable ent) {
       try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_VariableInsertar(?,?)}");              
            //oCst.setInt(1, ent.getCod_var_in());
            oCst.setString(1, ent.getNom_var_vc());
            oCst.setString(2, ent.getVal_var_vc());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(Variable ent) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_VariableEditar(?,?,?)}");              
            oCst.setInt(1, ent.getCod_var_in());
            oCst.setString(2, ent.getNom_var_vc());
            oCst.setString(3, ent.getVal_var_vc());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(int nCodigo) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_VariableEliminar(?)}");              
            oCst.setInt(1, nCodigo);           
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
